/**
 * =========================== 通用网络请求方法 ===========================
 */
//加入下面的代码
// document.write("<script language=javascript src='js/constant.js'></script>");
// document.write("<script language=javascript src='../js/constant.js'></script>");
// document.write("<script language=javascript src='../../js/constant.js'></script>");

function post(url, params, showLoading, onResponseSuccess, onResponseCodeError, onError) {
    request(url, "post", params, showLoading, onResponseSuccess, onResponseCodeError, onError);
}

function get(url, params, showLoading, onResponseSuccess, onResponseCodeError, onError) {
    request(url, "get", params, showLoading, onResponseSuccess, onResponseCodeError, onError);
}

function request(url, method, params, showLoading, onResponseSuccess, onResponseCodeError, onError) {
    if (showLoading)
        $.showLoading();
    // var token = Cookies.get('token');
    $.ajax({
        type: method,
        // url: API.baseUrl + url,
        url: document.location.protocol + "//" + window.location.host + "/" + API.projectName + url,
        data: params,
        async: true,
        //  默认情况下，标准的跨域请求是不会发送cookie的
        dataType: "json",
        beforeSend: function (xhr) {
            xhr.withCredentials = true;
        },
        crossDomain: true,
        success: function (res, status, xhr) {
            // if (showLoading)
            // $.hideLoading();
            // console.log(res);
            if (res.code == 200) {
                onResponseSuccess(res);
            } else {
                // $.toptip(res.message);
                onResponseCodeError(res.code + " - " + res.message);
                // $.toast("访问错误：" + data.result, "forbidden");
            }
        },
        error: function (res) {
            // $.toptip(data);
            onError(res.status);
        }
    });
}


